package service

import (
	"crypto/md5"
	"errors"
	"fmt"
	"gorm.io/gorm"
	"tdy-backend/app/core/service"
	"tdy-backend/app/member/dto"
	"tdy-backend/app/member/model"
	"tdy-backend/global"
	"tdy-backend/utils"
	"time"
)

type MemberService struct {
	service.BaseService
}

//注册
func (o *MemberService) Register(registerData dto.RegisterDto) (bool, error) {
	encrypt := utils.RandAllString(8)
	member := model.Member{}
	db := o.DB
	memberInfo := db.Where("login_name=?", registerData.LoginName).First(&member)
	if memberInfo.Error == nil {
		return false, errors.New("账户已存在")
	}
	member.LoginName = registerData.LoginName
	member.Encrypt = encrypt
	createPasswordEncrypt := md5.Sum([]byte(registerData.Password + global.TYD_CONFIG.GetString("app.key") + encrypt))
	member.Password = fmt.Sprintf("%x", createPasswordEncrypt)
	result := db.Create(&member)
	if result.Error != nil {
		return false, result.Error
	}
	return true, nil
}

//登录
func (o *MemberService) Login(loginData dto.LoginDto) (string, error) {
	member := model.Member{}
	db := o.DB
	memberInfo := db.Where("login_name=?", loginData.LoginName).First(&member)
	if memberInfo.Error != nil {
		return "", errors.New("账户不存在")
	}
	createPasswordEncrypt := md5.Sum([]byte(loginData.Password + global.TYD_CONFIG.GetString("app.key") + member.Encrypt))
	tmpPassword := fmt.Sprintf("%x", createPasswordEncrypt)
	if member.Password != tmpPassword {
		return "", errors.New("账户或密码有误，请核对")
	}
	token, err := utils.GenerateToken(member.LoginName, time.Now().Add(24*time.Hour).Unix())
	if err != nil {
		return "", err
	}

	return token, nil

}

//获取用户信息
func (o *MemberService) GetCurrentMemberInfo(tokenString string) (*model.Member, error) {
	cc, err := utils.ParseToken(tokenString)
	if err != nil {
		return nil, err
	}
	db := o.DB
	member := model.Member{}
	db.Select("id", "login_name").Where("login_name=?", cc.Relation).First(&member)

	return &member, nil
}

//修改密码
func (o *MemberService) UpdatePassword(memberId uint, password string, oldPassword string) (bool, error) {
	db := o.DB
	member := model.Member{}
	memberInfo := db.Where("id=?", memberId).First(&member)
	if errors.Is(memberInfo.Error, gorm.ErrRecordNotFound) {
		return false, errors.New("账号不存在")
	}
	createPasswordEncryptOl := md5.Sum([]byte(oldPassword + global.TYD_CONFIG.GetString("app.key") + member.Encrypt))
	tmpPassword := fmt.Sprintf("%x", createPasswordEncryptOl)
	if member.Password != tmpPassword {
		return false, errors.New("旧密码有误，请核对")
	}
	encrypt := utils.RandAllString(8)
	createPasswordEncrypt := md5.Sum([]byte(password + global.TYD_CONFIG.GetString("app.key") + encrypt))
	member.Password = fmt.Sprintf("%x", createPasswordEncrypt)
	member.Encrypt = encrypt
	save := db.Select("password", "encrypt").Save(member)
	if save.Error != nil {
		return false, save.Error
	}
	return true, nil
}

func (o MemberService) Test() {
	db := o.DB
	fmt.Printf("dbTest：%p", db)
	//db := o.Base.GetDB()
	//group1 := model2.Group{Title: "啊哈哈哈哈", WorksId: 21}
	//fmt.Printf("dbTest：%p", o.db)
	//o.db.Create(&group1)
}

func (o MemberService) Test2() {
	//db := o.Base.GetDB()
	//group1 := model2.Group{Title: "jjjjj", WorksId: 21}
	//fmt.Printf("dbTest2：%p", o.db)
	//o.db.Create(&group1)
	//fmt.Printf("serviceTest:%p", db)
}
